package com.wangling.learn.endpoint;

import javax.websocket.Session;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimerTask;

public class SendNowTimerTask extends TimerTask {
    private final Session session;

    public SendNowTimerTask(Session session) {
        this.session = session;
    }

    @Override
    public void run() {
        try {
            LocalDateTime now = LocalDateTime.now();

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String s = formatter.format(now);

            // 通过session 对象，将当前时间，发送给远端（客户端）
            session.getBasicRemote().sendText(s);
        } catch (IOException exc) {
            // 重写方法时，无法修改方法签名，所以，不能抛出异常，只能自己catch住
            exc.printStackTrace(System.out);
        }
    }
}
